<template>
	<view class="page-body">
		<!-- 页面内容开始 -->

		<!-- 表格搜索组件开始 -->
		<vk-data-table-query v-model="queryForm1.formData" :columns="queryForm1.columns" @search="search">
		</vk-data-table-query>
		<!-- 表格搜索组件结束 -->

		<!-- 自定义按钮区域开始 -->
		<view>
			<el-row>
				<el-button type="success" size="small" icon="el-icon-download" @click="exportExcel">
					导出本页数据
				</el-button>
				<el-button type="success" size="small" icon="el-icon-download" @click="exportAll">
					导出所有数据
				</el-button>
			</el-row>
		</view>
		<!-- 自定义按钮区域结束 -->

		<!-- 表格组件开始 -->
		<vk-data-table ref="table1" :action="table1.action" :columns="table1.columns" :query-form-param="queryForm1"
			:right-btns="['detail_auto','update']" :selection="true" :row-no="true" :pagination="true"
			@update="updateBtn" @delete="deleteBtn" @current-change="currentChange" @selection-change="selectionChange">
			<template v-slot:pay_type="{ row, column, index }">
				<el-tag effect="dark" size="small" v-if="row.pay_type == 'alipay'">支付宝</el-tag>
				<el-tag effect="dark" size="small" v-if="row.pay_type == 'wxpay'" type="success">微信</el-tag>
				<el-tag effect="dark" size="small" v-if="row.pay_type == 'send'" type="info">赠送</el-tag>
				<el-tag effect="dark" size="small" v-if="row.pay_type == 'coin'" type="warning">学习币</el-tag>
				<el-tag effect="dark" size="small" v-if="row.pay_type == 'transfer'" type="info">转账</el-tag>
				<view v-if="row.pay_type == null">暂无</view>
			</template>
		</vk-data-table>
		<!-- 表格组件结束 -->

		<!-- 添加或编辑的弹窗开始 -->
		<vk-data-dialog v-model="form1.props.show" :title="form1.props.title" width="500px" mode="form"
			:close-on-click-modal="false">
			<vk-data-form v-model="form1.data" :rules="form1.props.rules" :action="form1.props.action"
				:form-type="form1.props.formType" :columns='form1.props.columns' label-width="60px"
				@success="form1.props.show = false;refresh();"></vk-data-form>
		</vk-data-dialog>
		<!-- 添加或编辑的弹窗结束 -->

		<!-- 页面内容结束 -->
	</view>
</template>

<script>
	var that; // 当前页面对象
	var vk = uni.vk; // vk实例
	var originalForms = {}; // 表单初始化数据
	var payStatus = [{
		"label": "待付款",
		"value": 1
	}, {
		"label": "已付款",
		"value": 2
	}, {
		"label": "退款审核中",
		"value": 3
	}, {
		"label": "退款中",
		"value": 4
	}, {
		"label": "已退款",
		"value": 5
	}, {
		"label": "已取消",
		"value": -1
	}, {
		"label": "退款拒绝",
		"value": -2
	}, {
		"label": "退款失败",
		"value": -3
	}]
	var getStatus = [{
		"label": "所有",
		"value": ''
	}, {
		"label": "支付宝",
		"value": 'alipay'
	}, {
		"label": "微信",
		"value": 'wxpay'
	}, {
		"label": "赠送",
		"value": 'send'
	}, {
		"label": "学习币",
		"value": 'coin'
	}]
	var typeStatus = [{
		"label": "全部",
		"value": ''
	}, {
		"label": "课程",
		"value": 'course'
	}, {
		"label": "充值学习币",
		"value": 'recharge'
	}]

	export default {
		data() {
			// 页面数据变量
			return {
				// 页面是否请求中或加载中
				loading: false,
				// init请求返回的数据
				data: {

				},
				// 表格相关开始 -----------------------------------------------------------
				table1: {
					// 表格数据请求地址
					action: "admin/order/list/sys/getList",
					// 表格字段显示规则
					columns: [
						{ key: "order_no", title: "单号", type: "text", width: 220 },
						{ key: "title", title: "订单", type: "text", width: 300 },
						{ key: "total_fee", title: "金额", type: "money", width: 100 },
						{ key: "user.nickname", title: "用户", type: "text", width: 150 },
						{ key: "user.mobile", title: "联系电话", type: "text", width: 150 },
						{ key: "pay_type", title: "付款方式", type: "text", width: 120 },
						{
							key: "type",
							title: "订单类型",
							type: "html",
							width: 100,
							formatter: function(val, row, column, index) {
								switch (val) {
									case 'course':
										return '<text>课程</text>';
									case 'recharge':
										return '<text>充值学习币</text>';
								}
							}
						}, {
							key: "status",
							title: "状态",
							type: "html",
							width: 120,
							formatter: function(val, row, column, index) {
								let status = payStatus.find(v => v.value == val)
								let color = '#606266'
								switch (val) {
									case 2:
										color = '#42b983'
										break
									case 5:
										color = 'red'
										break
								}
								return '<text style="color:' + color + '">' + (status.label || '未知') + '</text>';
							}
						}, {
							key: "orderInfo.address_info",
							title: "地址信息",
							type: "html",
							show: ['detail'],
							formatter: function(val, row, column, index) {
								if (vk.pubfn.isNull(val)) return '暂无地址'
								return `<text>${val.province}${val.city}${val.area}${val.detail}</text>`;
							}
						}, {
							key: "good.admin_send",
							title: "免费赠送",
							type: "html",
							width: 80,
							formatter: function(val, row, column, index) {
								switch (val) {
									case true:
										return '<text>是</text>'
									default:
										return '<text>否</text>'
								}
							}
						},
						{ key: "_add_time", title: "创建时间", type: "time", width: 160, sortable: "custom" },
						{ key: "comment", title: "备注", type: "time", show: ['detail'] }
					],
					// 多选框选中的值
					multipleSelection: [],
					// 当前高亮的记录
					selectItem: ""
				},
				// 表格相关结束 -----------------------------------------------------------
				// 表单相关开始 -----------------------------------------------------------
				// 查询表单请求数据
				queryForm1: {
					// 查询表单数据源，可在此设置默认值
					formData: {},
					// 查询表单的字段规则 fieldName:指定数据库字段名,不填默认等于key
					columns: [
						{ key: "order_no", title: "单号", type: "text", mode: "="},
						{
							key: "mobile",
							title: "手机号",
							type: "text",
							mode: "%%",
							fieldName: "user.mobile",
							lastWhereJson: true
						},
						{ key: "status", title: "订单状态", type: "select", mode: "=", "data": payStatus },
						{ key: "_add_time", title: "添加时间", type: "datetimerange", mode: "[]" },
						{
							key: "pay_type",
							title: "支付方式",
							type: "select",
							mode: "=",
							data: getStatus
						},
						{
							key: "type",
							title: "类型",
							type: "select",
							mode: "=",
							data: typeStatus
						},
					]
				},
				form1: {
					"data": {
						"sort": 50,
						"status": true
					},
					"props": {
						"action": "请替换为自己的路由地址",
						"columns": [{
							"key": "order_no",
							"title": "单号",
							"type": "text",
							"disabled": true,
							"showLabel": true
						}, {
							"key": "total_fee",
							"title": "金额",
							"type": "money",
							"placeholder": "请输入金额",
							"showLabel": true
						}, {
							"key": "comment",
							"title": "备注",
							"type": "textarea",
							"placeholder": "请输入备注",
							"showLabel": true
						}],
						"rules": {
							"title": [{
								"required": true,
								"message": "标题不能为空",
								"trigger": "change"
							}]
						},
						"formType": "",
						"show": false,
						"loading": false
					}
				},
				// 其他弹窗表单
				formDatas: {},
				// 表单相关结束 -----------------------------------------------------------
			};
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady() {},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options) {
				originalForms["form1"] = vk.pubfn.copyObject(that.form1);
			},
			// 页面跳转
			pageTo(path) {
				vk.navigateTo(path);
			},
			// 表单重置
			resetForm() {
				vk.pubfn.resetForm(originalForms, that);
			},
			// 搜索
			search() {
				that.$refs.table1.search();
			},
			// 刷新
			refresh() {
				that.$refs.table1.refresh();
			},
			// 获取当前选中的行的数据
			getCurrentRow() {
				return that.$refs.table1.getCurrentRow();
			},
			// 监听 - 行的选中高亮事件
			currentChange(val) {
				that.table1.selectItem = val;
			},
			// 当选择项发生变化时会触发该事件
			selectionChange(list) {
				that.table1.multipleSelection = list;
			},
			// 显示修改页面
			updateBtn({ item }) {
				that.form1.props.action = 'admin/order/list/sys/update';
				that.form1.props.formType = 'update';
				that.form1.props.title = '编辑订单';
				that.form1.props.show = true;
				that.form1.data = item;
			},
			exportExcel() {
				this.$refs.table1.exportExcel();
			},
			exportAll() {
				this.$refs.table1.exportExcel({
					fileName: "表格全部数据",
					title: "正在导出数据...",
					pageIndex: 1,
					pageSize: -1 // 此值为-1，代表导出所有数据
				});
			}
		},
		// 监听属性
		watch: {

		},
		// 过滤器
		filters: {

		},
		// 计算属性
		computed: {

		}
	};
</script>
<style lang="scss" scoped>
	.page-body {}

	.color2 {
		color: #42b983 !important;
	}

	.color5 {
		color: red !important;
	}
</style>
